﻿using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Collections;

public class mapScene : MonoBehaviour, ICardboardGazeResponder
{
    WWW www;// Request
    string filePath;// File saved path and directory
    Texture2D map;// Map downloaded from SuperMap
    HelpURLAttribute URL = new HelpURLAttribute("http://112.126.69.154:8090/iserver/output/temp/WHUmap_8aex98c/N24/2x2/1111x1056_391148291.png?_t=1470567808594");
    float speed = 1;
    public bool isMove { set; get; }
    bool gazeEnter = false;

    // Use this for initialization
    void Start()
    {
        isMove = true;
        filePath = Application.dataPath + @"/Resources/Map/WHUmap.jpg";// Map saved path
        if (!File.Exists(filePath))// If the picture doesn't exist, download it
        {
            StartCoroutine(LoadImg(URL));
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (isMove)
        {
            BoundaryHead();
            MoveHead(speed);
        }
    }

    IEnumerator LoadImg(HelpURLAttribute url)// Download picture
    {
        www = new WWW(url.URL);
        yield return www;

        map = www.texture;
        byte[] picByte = map.EncodeToJPG();
        File.WriteAllBytes(filePath, picByte);
    }

    void MoveHead(float speed)
    {
        // Move the head
        GameObject head = GameObject.FindGameObjectWithTag("head");
        GameObject camera = GameObject.FindGameObjectWithTag("camera");
        Vector3 positionCamera = new Vector3();
        Vector3 rotationHead = new Vector3();
        positionCamera = camera.GetComponent<Transform>().position;
        rotationHead = head.GetComponent<Transform>().localRotation.eulerAngles;

        // Control the angle of moving
        if (rotationHead.x <= 360 && rotationHead.x >= 270) positionCamera.y += Time.deltaTime * (speed / 3 + (360 - rotationHead.x) * 1.0f / 20) * speed;
        if (rotationHead.x >= 0 && rotationHead.x <= 90) positionCamera.y -= Time.deltaTime * (speed / 3 + rotationHead.x * 1.0f / 20) * speed;
        if (rotationHead.y <= 360 && rotationHead.y >= 270) positionCamera.x -= Time.deltaTime * (speed / 3 + (360 - rotationHead.y) * 1.0f / 20) * speed;
        if (rotationHead.y >= 0 && rotationHead.y <= 90) positionCamera.x += Time.deltaTime * (speed / 3 + rotationHead.y * 1.0f / 20) * speed;
        camera.GetComponent<Transform>().position = positionCamera;
    }

    void BoundaryHead()
    {
        // Set the boundary so that user can not fly out of the map
        GameObject head = GameObject.FindGameObjectWithTag("head");
        GameObject camera = GameObject.FindGameObjectWithTag("camera");
        Vector3 position = new Vector3();
        position = camera.GetComponent<Transform>().position;

        if (position.x >= 10) position.x = 10;
        else if (position.x <= -10) position.x = -10;

        if (position.y >= 10) position.y = 10;
        else if (position.y <= -10) position.y = -10;

        camera.GetComponent<Transform>().position = position;
    }

    public void ButtonAdd()
    {
        // Increase the speed of moving
        if (++speed >= 3) speed = 3;
        GameObject speedButton = GameObject.FindGameObjectWithTag("btnSpeed");
        speedButton.GetComponentInChildren<Text>().text = "当前速度:" + speed.ToString();
    }

    public void ButtonMinus()
    {
        // Decrease the speed of moving
        if (--speed <= 0) speed = 0;
        GameObject speedButton = GameObject.FindGameObjectWithTag("btnSpeed");
        speedButton.GetComponent<Text>().text = "当前速度:" + speed.ToString();
    }

    public void ButtonStop()
    {
        // Stop moving
        speed = 0;
        GameObject.FindGameObjectWithTag("canvas").GetComponent<mapFly>().isFly = false;
        GameObject speedButton = GameObject.FindGameObjectWithTag("btnSpeed");
        speedButton.GetComponentInChildren<Text>().text = "当前速度:" + speed.ToString();
    }

    public void ButtonReset()
    {
        // Go back to the start position
        GameObject camera = GameObject.FindGameObjectWithTag("camera");
        Vector3 positionCamera = new Vector3();
        positionCamera.x = 0; positionCamera.y = 1; positionCamera.z = 0;
        camera.GetComponent<Transform>().position = positionCamera;
    }

    public void ButtonNear()
    {
        GameObject camera = GameObject.FindGameObjectWithTag("camera");
        Vector3 positionCamera = new Vector3();
        positionCamera = camera.GetComponent<Transform>().position;
        positionCamera.z++;
        if (positionCamera.z >= 4) positionCamera.z = 4;
        camera.GetComponent<Transform>().position = positionCamera;
    }

    public void ButtonFar()
    {
        GameObject camera = GameObject.FindGameObjectWithTag("camera");
        Vector3 positionCamera = new Vector3();
        positionCamera = camera.GetComponent<Transform>().position;
        positionCamera.z--;
        if (positionCamera.z <= -3) positionCamera.z = -3;
        camera.GetComponent<Transform>().position = positionCamera;
    }

    public void VRmode()
    {
        // Stop the VR mode
        Cardboard.SDK.VRModeEnabled = !Cardboard.SDK.VRModeEnabled;
    }

    public void OnGazeEnter()
    {
    }

    public void OnGazeExit()
    {
    }

    public void OnGazeTrigger()
    {
    }
}
